package xyz.liuyuhe.StudySpring.ch9.demo3;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ibatis.common.jdbc.ScriptRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/ch9/applicationContext3.xml")
public class Demo3 {
    
    @Autowired
    private AccountService accountService;
    
    @Autowired
    private DataSource dataSource;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }
    
    @Before
    public void setup() throws SQLException, FileNotFoundException, IOException{
        Connection conn = dataSource.getConnection();
        ScriptRunner sr = new ScriptRunner(conn, true, false);
        sr.runScript(new FileReader("./src/main/resources/ch9/init.sql"));
        conn.close();
    }
    
    @After
    public void tearDown() throws SQLException, FileNotFoundException, IOException{
        Connection conn = dataSource.getConnection();
        ScriptRunner sr = new ScriptRunner(conn, true, false);
        sr.runScript(new FileReader("./src/main/resources/ch9/teardown.sql"));
        conn.close();
    }
    
    @Test
    public void testTransfer(){
        accountService.transfer("aaa", "bbb", 100d);
        System.out.println("转账正常完成");
    }
}